package com.zhangye.test.api;

import java.util.HashMap;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.zhangye.test.model.ProxyAr;
import com.zhangye.test.model.ProxyQrCode;
import com.zhangye.test.service.ProxyArService;
import com.zhangye.test.service.ProxyQrCodeService;
import com.zhangye.test.util.MatrixToImageWriter;

@Controller
@RequestMapping("api/QrCode")
public class QrCodeApi {
	
	@Autowired
	private ProxyArService proxyArService;
	
	@Autowired
	private ProxyQrCodeService proxyQrCodeService;


    @RequestMapping(value = "create/{id}", method = RequestMethod.GET)
    public String qrcode(@PathVariable("id") Long id, HttpServletResponse response) throws Exception {
        String text = "https://ar.zhyell.com/api/QrCode/show/" + id;
        int width = 1024;
        int height = 1024;
        //二维码的图片格式
        String format = "png";
        

		//定义二维码的参数
		HashMap hints = new HashMap<>();
		hints.put(EncodeHintType.CHARACTER_SET,"utf-8"); //编码格式
		hints.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.M); //容错等级
		hints.put(EncodeHintType.MARGIN,0);	//边距
        BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);
        //生成二维码
        MatrixToImageWriter.writeToStream(bitMatrix, format, response.getOutputStream());
        return null;
    } 

    @RequestMapping(value = "show/{id}")
    public ModelAndView show(@PathVariable("id") Long id, Model model) {
    	ProxyAr p = proxyArService.selectByPrimaryKey(id);
    	String videoUrl = "redirect:";
        if(p != null && !"".equals(p)){
        	videoUrl += p.getVideoUrl();
        }else{
        	videoUrl += "https://www.baidu.com";
        }
        model.addAttribute("videoUrl",videoUrl);
//        return "proxyAr/video";
        return new ModelAndView(videoUrl);
    } 


    @RequestMapping(value = "create1/{id}", method = RequestMethod.GET)
    public String create1(@PathVariable("id") Long id, HttpServletResponse response) throws Exception {
        String text = "https://ar.zhyell.com/api/QrCode/show1/" + id;
        int width = 1024;
        int height = 1024;
        //二维码的图片格式
        String format = "png";
        

		//定义二维码的参数
		HashMap hints = new HashMap<>();
		hints.put(EncodeHintType.CHARACTER_SET,"utf-8"); //编码格式
		hints.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.M); //容错等级
		hints.put(EncodeHintType.MARGIN,0);	//边距
        BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);
        //生成二维码
        MatrixToImageWriter.writeToStream(bitMatrix, format, response.getOutputStream());
        return null;
    } 

    @RequestMapping(value = "show1/{id}")
    public ModelAndView show1(@PathVariable("id") Long id, Model model) {
    	ProxyQrCode pqc = proxyQrCodeService.selectByPrimaryKey(id);
    	String videoUrl = "redirect:";
        if(pqc != null && !"".equals(pqc)){
        	videoUrl += pqc.getVideoUrl();
        }else{
        	videoUrl += "https://www.baidu.com";
        }
        model.addAttribute("videoUrl",videoUrl);
//        return "proxyAr/video";
        return new ModelAndView(videoUrl);
    } 

}
